// const { ajax } = require("jquery");

window.onload = () => {
    let url_n = window.location.search
   let tel_d = url_n.slice(url_n.indexOf("?")+1,url_n.indexOf("="))

    if(localStorage.getItem(`${tel_d}`)){
        $('.foot').css({
            display: 'none'
        })
        $('.master').css({
            display: 'none'
        })
    }else{
        localStorage.setItem(`${tel_d}`,tel_d)
    }
    


  
    // 关闭模态框和遮盖层
    document.querySelector(".foot .close").onclick = () => {
        document.querySelector(".foot").style.transition=".8s"
        document.querySelector(".master").style.transition=".8s"
        document.querySelector(".foot").style.transform="translateX(-200%)"
        document.querySelector(".master").style.transform="translateX(-100%)"
        setTimeout(() =>{
            document.querySelector(".foot").style.display = "none";
            document.querySelector(".master").style.display = "none";
        },500)
        
    }
    // 点击设置弹出设置框
    document.querySelector(".set").onclick = () => {
        let value=document.querySelector(".sets").style.display;
        if(value=='none'){
            document.querySelector(".sets").style.display = "block";
        }else{
            document.querySelector(".sets").style.display = "none";
        }
    }
    let setlist = document.querySelector(".sets li");
    for (let i = 0; i < setlist.length; i++) {
        setlist[i].onclick = () => {
            document.querySelector(".sets").style.display = "none";
        }
    }
    // 选项卡
    let pagelist = document.querySelectorAll(".page li");
    let tablist = document.querySelectorAll(".tab li");
    for (let i = 0; i < pagelist.length; i++) {
        pagelist[i].onclick = () => {
           
                tablist.forEach(v=>{
                    v.style.display = "none";
                    $(tablist[i]).css("display", "block")
                    $(v).css({
                        transform:'translateY(100%)',
                        transition:'.8s'
                     })
                  })
         
             
            
                 setTimeout(() => {
                    $(tablist[i]).css({
                        transform:'translateY(0%)',
                        transition:'.8s'
                     })
                 },50)
            
            $(pagelist[i]).css("color", "#fff");
            $(pagelist[i]).css("backgroundImage", "linear-gradient(to left,#4463FF,#11C1FE)");
            $(pagelist[i]).siblings("li").css({ "color": "#ccc", "backgroundImage": "linear-gradient(to left,#fff,#fff)" });
           
          
        }
    }
    // 点击头像修改资料
    document.querySelector(".touxiang").onclick = () => {
        setTimeout(() => {
            document.querySelector(".master").style.transition=".8s"
            document.querySelector(".outer").style.transition=".8s"
            document.querySelector(".master").style.transform="translateX(0%)"
            document.querySelector(".outer").style.transform="translateX(0%)"
        },70)
        document.querySelector(".master").style.transform="translateX(-100%)"
        document.querySelector(".outer").style.transform="translateX(-200%)"
        
        document.querySelector(".master").style.display = "block";
        document.querySelector(".outer").style.display = "block";
    }
    // 点击关闭按钮关闭修改资料框
    document.querySelector(".outer .close").onclick = () => {
        document.querySelector(".master").style.transform="translateX(100%)"
        document.querySelector(".outer").style.transform="translateX(200%)"
        document.querySelector(".master").style.transition=".8s"
         document.querySelector(".outer").style.transition=".8s"
        setTimeout(() => {
            document.querySelector(".outer").style.display = "none";
            document.querySelector(".master").style.display = "none";
        }
        
        ,800)
        
       
    }

    //考试跳转
    $('#t2').on('click', function () {
        let student_idu = window.location.search
        let student_id = student_idu.slice(student_idu.indexOf("?") + 1);
        let type = $('#t2').attr("name")
        window.location.href = `exam_type.html?${student_id}&${type}`
    })
    $('#t1').on('click', function () {
        let student_idu1 = window.location.search
        let student_id1 = student_idu1.slice(student_idu1.indexOf("?") + 1);
        let type = $('#t1').attr("name")
        window.location.href = `exam_type.html?${student_id1}&${type}`
    })
    $('#t3').on('click', function () {
        let student_idu3 = window.location.search
        let student_id3 = student_idu3.slice(student_idu3.indexOf("?") + 1);
        let type = $('#t3').attr("name")
        window.location.href = `exam_type.html?${student_id3}&${type}`
    })
    $('#t4').on('click', function () {
        let student_idu4 = window.location.search
        let student_id4 = student_idu4.slice(student_idu4.indexOf("?") + 1);
        let type = $('#t4').attr("name")
        window.location.href = `exam_type.html?${student_id4}&${type}`
    })
    $('#t5').on('click', function () {
        let student_idu5 = window.location.search
        let student_id5 = student_idu5.slice(student_idu5.indexOf("?") + 1);
        let type = $('#t5').attr("name")
        window.location.href = `exam_type.html?${student_id5}&${type}`
    })

    // 换个账号登录
    // document.querySelector(".head .sets li:nth-of-type(1)").onclick=()=>{
    //     document.querySelector(".master").style.display="block";
    //     document.querySelector(".changeUser").style.display="block"
    //     student();
    // }
    // document.querySelector(".change").onclick=()=>{
    //     document.querySelector(".master").style.display="none";
    //     document.querySelector(".changeUser").style.display="none"
    //     document.querySelector(".sets").style.display="none"
    // }
    // 退出登录跳转
    document.querySelector(".head .sets li:nth-of-type(2)").onclick = () => {
        if (window.confirm("您确认退出登录吗？")) {
            window.location.href = "login.html"
        }
    }


    mesAjax()
}


let mesAjax = async () => {
    let res = await ajax({
        type: 'get',
        url: 'http://127.0.0.1:4000/xgstudent/GetAllStudents',
        data: {}
    })
   
    let url = window.location.href;
    let mes = url.slice(url.indexOf("?") + 1,url.indexOf("="));
    let ress = JSON.parse(res);
    
    // console.log(ress.obj);
    // console.log(ress);
    ress.obj.forEach(item => {
        // console.log(item.name);
        if (item.phone == mes) {
         
            document.querySelector('.names').value = item.name;
            document.querySelector('.gender1').value = item.gender;
            document.querySelector('.name').innerText = item.name;
        }
    })

}




// 获取所有学生信息
let student = async () => {
    let result = await ajax({
        type: "get",
        url: "http://127.0.0.1:4000/xgstudent/GetAllStudents"
    })
    let { code, obj } = JSON.parse(result);
    let namearr = [];
    let phonearr = [];
    let avatararr = [];
    let student = ''
    if (code == 200) {
        for (let item of obj) {
            namearr.push(item.name);
            phonearr.push(item.phone);
            avatararr.push(item.avatar);
        }
        console.log(namearr, phonearr, avatararr);
        for (let j = 0; j < namearr.length; j++) {
            let students = `
            <p>
            <span class="name">姓名:${namearr[j]}
            <span class="phone">电话:${phonearr[j]}
            <span class="avatar">头像:<img src="">
            </p>
            `;
            student += students
        }
        document.querySelector(".changeUser").innerHTML += student

    }

}